<template>
	<view class="container">
		<view class="space">

		</view>
		<uni-section title="下单信息" type="line">
			<template v-slot:right>
				<view>{{order.id}}</view>
			</template>
			<uni-list>
				<uni-list-item :title="order.userName" :note="formatDateTime(order.createTime)"
					:thumb="order.avatar || '/static/logo.png'" thumb-size="lg"
					:right-text="orderNum(order)"></uni-list-item>
			</uni-list>
		</uni-section>
		<view class="space">

		</view>
		<uni-section title="订单内容" type="circle">
			<template v-slot:right>
				<view class="header-status" v-if="order.consumeOrderState == 1">新订单</view>
			</template>
			<uni-list>
				<uni-list-item :title="item.goodsName"
					:note="priceTransform(item.buyPrice) + ' ' + (item.goodsNum || 0) + (item.goodsSpec || '')"
					:thumb="imageSrc(item.goodsPictures)" thumb-size="lg" :right-text="priceTransform(item.totalPrice)"
					v-for="item in order.orderDetailVos" :key="item.id">
				</uni-list-item>
			</uni-list>

			<view class="order-jine">
				<view>订单金额</view>
				<view>合计 {{priceTransform(order.orderMoney)}}</view>
			</view>
			<view class="line"></view>

			<view class="order-account-info">
				<view class="order-account order-qianbaokouchu">
					<view>消费折返</view>
					<view class="order-kouchu-text"> -{{priceTransform(order.backMoney)}}</view>
				</view>
				<view class="order-account order-shijizhifu">
					<view>实际收入</view>
					<view class="order-zhifu-text">{{priceTransform(order.incomeMoney)}}</view>
				</view>
			</view>
			<view class="line"></view>
			<view class="order-opre" v-if="order.consumeOrderState == 20">
				<button type="primary" size="mini" @click="confirmAndPrintOrder">打印</button>
			</view>
		</uni-section>

	</view>
</template>

<script>
	import {
		getSummaryOrderDetail,
		confirmOrder,
		getMyShopName
	} from '@/api/shops/preOrderDetail/preOrderDetail.js'
	import {
		transform
	} from '@/minix/mixin.js'

	export default {
		mixins: [transform],
		data() {
			return {
				orderId: "",
				order: {},
				shopName: '',
			}
		},
		onLoad: function(option) {

			this.orderId = option.id
			this.init()
		},
		methods: {
			init() {
				let _this = this
				getSummaryOrderDetail(this.orderId).then((res) => {

					this.order = res

				})
				getMyShopName().then((res) => {

					this.shopName = res

				})
				bluetooth.reconnect()

			},
			openPop() {
				this.$refs.popup.open();
			},
			orderNum(item) {
				if (item.serialNum) {
					return '流水号:' + item.serialNum.toString().padStart(4, '0')
				}
				if (item.deskName) {
					return '桌号:' + item.deskName
				}
				return '';
			},
			confirmOrder() {
				let _this = this;
				uni.showModal({
					title: '确定接单?',
					success: (res) => {
						if (res.confirm) {
							confirmOrder(this.orderId).then((rs) => {

								uni.showToast({
									title: '接单成功'
								})
								uni.navigateBack({
									success: function() {
										_this.getOpenerEventChannel().emit(
											"needRefreshPage", true)
									}
								})

							})
						}
					}

				})
			},
			confirmAndPrintOrder() {
				let _this = this
				uni.showModal({
					title: '确定接单并打印?',
					success: (res) => {
						if (res.confirm) {
							uni.showToast({
								title: "正在为您打印...",
								mask: true,
								icon: "none",
								duration: 2000
							})
							bluetooth.notifyBLECharacteristicValue();
							setTimeout(() => {
								_this.writeBLECharacteristicValue();
							}, 500);
							// confirmOrder(this.orderId).then((rs) => {
							// 	if(rs.code == 200) {
							// 		uni.showToast({
							// 			title:'接单成功'
							// 		})
							// 	}
							// })
						}
					}

				})
			},
		}
	}
</script>

<style lang="scss">
	.space {
		margin-top: 10px;
	}

	.orderin-info {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		background-color: #fff;
		margin: 10px 0;
		padding: 10px 0;

		.order-person {
			display: flex;
			flex-direction: column;
			align-items: flex-start;

			.person-content {
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
			}
		}

		.user-image {
			width: 50px;
			height: 50px;
		}
	}



	.order-status {
		background-color: #e8f2e1;
		height: 50px;
		line-height: 50px;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #94c293;
		font-weight: bold;
	}

	.order-detail-header {
		display: flex;
		flex-direction: row;
		background-color: #fff;
		padding: 10px;


		image {
			width: 100rpx;
			height: 100rpx;
		}

		.order-shop {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			flex: 1;
			margin-left: 10px;

			.order-shop-name {
				font-size: 14px;
				font-weight: bold;
			}

			.order-shop-open-time {
				font-size: 12px;
				color: #cfc9c5;
			}
		}

		.order-shop-disc {
			display: flex;
			flex-direction: row;
			justify-content: flex-end;
			border-radius: 5px;
			padding: 5px;
			background-color: #fff1db;
			color: #ffac46;
			font-weight: bold;
			line-height: 1;
		}
	}

	.order-price-detail {
		.order-header {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			padding: 10px;
			background-color: #fafcff;

			.header-tip {}

			.header-status {
				color: red;
				font-weight: bold;
			}
		}


		.order-price-title {
			font-size: 13px;
			font-weight: bold;
			margin: 10px 0;
			margin-left: 10px;

		}

		.order-price-content {
			display: flex;
			flex-direction: column;
			background-color: #fff;

			.order-price-content-item {
				display: flex;
				flex-direction: row;

				padding: 10px;

				image {
					width: 100rpx;
					height: 100rpx;
				}

				.order-price-intro {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					flex: 1;
					margin-left: 10px;

					.order-price-name {
						font-size: 14px;
						font-weight: bold;
					}

					.order-price-guige {
						font-size: 12px;
						color: #cfc9c5;
					}
				}

				.order-price-price {
					font-size: 14px;
					font-weight: bold;
					display: flex;
					flex-direction: row;
					justify-content: flex-end;
				}
			}

			.order-jine {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				padding: 0 10px;
				font-weight: 600;
			}

			.line {
				border-bottom: 1px solid #cfc9c5;
				margin: 10px 0;
			}

			.order-account-info {
				display: flex;
				flex-direction: column;
				font-weight: bold;
				border-bottom: 1px solid #ababab;

				.order-account {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					padding: 0 10px;
					margin: 5px 0;
				}

				.order-kouchu-text {
					color: red;
				}

				.order-zhifu-text {
					font-size: 17px;
				}

				.order-zhefan-text {
					font-size: 13px;
					color: #7bf30f;
				}

			}

			.order-opre {
				display: flex;
				flex-direction: row;
				justify-content: space-around;
				margin: 10px;

				.order-opre-btn {
					background-color: #7bf30f;
					color: #fff;
				}
			}
		}
	}

	.order-jine {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 0 10px;
		font-weight: 600;
		margin-top: 10px;
	}

	.line {
		border-bottom: 1px solid #cfc9c5;
		margin: 5px 0;
	}

	.order-account-info {
		display: flex;
		flex-direction: column;
		font-weight: bold;

		.order-account {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			padding: 0 10px;
			margin: 5px 0;
		}

		.order-kouchu-text {
			color: red;
		}

		.order-zhifu-text {
			font-size: 17px;
		}

		.order-zhefan-text {
			font-size: 13px;
			color: #7bf30f;
		}

	}

	.order-opre {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		margin: 10px;
		padding-bottom: 10px;

		.order-opre-btn {
			background-color: #7bf30f;
			color: #fff;
		}
	}


	.order-price-info {
		margin-top: 20px;
		background-color: #fff;
		padding: 10px;

		.order-price-info-title {
			font-weight: bold;
			margin-bottom: 10px;

		}

		.order-meta {
			font-size: 13px;
			color: #cfc9c5;
		}

	}

	// ==================================

	.content {
		background-color: #94c293;
		padding: 20px 0;
	}

	.devices_summary {
		margin-top: 5rpx;
		padding: 20rpx;
		font-size: 30rpx;
	}

	.device_list {
		margin: 5rpx 20rpx 5rpx 20rpx;
		border: 1rpx solid #ddd;
		border-radius: 10rpx;
		background-color: #FdFdFd;
		min-height: 0rpx;
		max-height: 400rpx;
		width: 700rpx;

	}

	.device_item {
		border-bottom: 1rpx solid #ddd;
		padding: 20rpx;
		color: #666;

	}

	.device_item_hover {
		background-color: rgba(0, 0, 0, .1);
	}

	.connected_info {
		position: fixed;
		bottom: 0;
		width: 100%;
		background-color: #F0F0F0;
		padding: 10px;
		padding-bottom: 20px;
		margin-bottom: env(safe-area-inset-bottom);
		font-size: 14px;
		min-height: 100px;
		box-shadow: 0px 0px 3px 0px;
	}

	.connected_info .operation {
		position: absolute;
		display: inline-block;
		right: 30px;
	}
</style>